package com.zjc.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjc.common.R;
import com.zjc.pojo.AbsentTable;
import com.zjc.pojo.HolidayTable;
import com.zjc.service.AbsentTableService;
import com.zjc.service.HolidayTableService;
import com.zjc.service.StudentService;
import com.zjc.service.VisitTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 周佳成
 * @since 2023-09-17
 */
@RestController
@RequestMapping("/api/holi")
public class HolidayTableController {
	@Autowired
	HolidayTableService holidayTableService;
	@Autowired
	StudentService studentService;
	@Autowired
	VisitTableService visitTableService;
	@GetMapping("/find")
	public R<Page> GetHoliPage(int pageNum, int pageSize, String search,Integer ldId){
		QueryWrapper<HolidayTable> wrapper = new QueryWrapper<>();
		if(search!=null){
			wrapper.like("stu_name",search);
		}
		System.out.println(ldId);
		wrapper.inSql("stu_id","select stu_id from student where ld_id = "+ldId);

		Page<HolidayTable> page1 = new Page<>(pageNum,pageSize);
		holidayTableService.page(page1,wrapper);
		return R.success(page1);
	}
	@PostMapping("/applyadd")
	public R<String> holiadd(@RequestBody HolidayTable ht){
		Integer stuid = ht.getStuId();
		String stuname = ht.getStuName();
		if(!studentService.isStuExists(stuid)){
			return R.error("学号不存在！！");
		}
		if(!visitTableService.isMatch4Stuid2name(stuid,stuname)){
			return R.error("学号姓名不匹配！！");
		}
		holidayTableService.save(ht);
		return R.success("登记成功！");
	}
}

